import { apiRequest } from './request'

// 订单接口类型定义
export interface Order {
    id?: number
    orderNo: string
    customerName: string
    customerPhone: string
    productName: string
    quantity: number
    totalAmount: number
    status: 'pending' | 'confirmed' | 'shipped' | 'delivered' | 'cancelled'
    createTime?: string
    updateTime?: string
}

export interface OrderQuery {
    page?: number
    pageSize?: number
    orderNo?: string
    customerName?: string
    status?: string
    startDate?: string
    endDate?: string
}

// 退款接口类型定义
export interface RefundOrder {
    id: number
    orderType: number
    orderId: string | null
    refundNo: string
    refundAmount: number
    refundSource: number
    refundStatus: number
    refundMethod: number
    createTime: string
    processedTime: string | null
    processedName: string | null
    remark: string
}

export interface RefundQuery {
    PageIndex: number
    PageSize: number
    Keyword?: string
    StartTime?: string
    EndTime?: string
    RefundStatus?: string
}

export interface RefundPageResponse {
    data: RefundOrder[]
    totalCount: number
    pageIndex: number
    pageSize: number
    totalPages: number
}

// 退款详情接口类型定义
export interface RefundDetail {
    id: number
    refundNo: string
    refundStatus: number
    refundStatusDesc: string
    createTime: string
    userPhone: string
    orderNo: string
    orderAmount: number
    refundAmount: number
    refundMethod: number
    refundMethodDesc: string
    orderType: number
    orderTypeDesc: string
    refundSource: number
    refundSourceDesc: string
    processedTime: string | null
    processedName: string | null
    remark: string
}

// 药品订单接口类型定义
export interface DrugOrder {
    id: number
    orderNo: string
    createTime: string
    phone: string
    consignee: string
    orderAmount: number
    orderStatus: number
    orderStatusName: string
    patientId: number
}

export interface DrugOrderQuery {
    PageIndex: number
    PageSize: number
    Keyword?: string
    StartTime?: string
    EndTime?: string
    OrderStatus?: number
}

export interface DrugOrderPageResponse {
    data: DrugOrder[]
    totalCount: number
    pageIndex: number
    pageSize: number
    totalPages: number
}

// 订单API类
export class OrderAPI {
    // 获取订单列表（读操作 - DU读）
    static async getOrderList(params: OrderQuery) {
        return apiRequest.get('/orders', params)
    }

    // 获取订单详情（读操作 - DU读）
    static async getOrderDetail(id: number) {
        return apiRequest.get(`/orders/${id}`)
    }

    // 创建订单（写操作 - XIE写）
    static async createOrder(order: Omit<Order, 'id' | 'createTime' | 'updateTime'>) {
        return apiRequest.post('/orders', order)
    }

    // 更新订单（写操作 - XIE写）
    static async updateOrder(id: number, order: Partial<Order>) {
        return apiRequest.put(`/orders/${id}`, order)
    }

    // 删除订单（写操作 - XIE写）
    static async deleteOrder(id: number) {
        return apiRequest.delete(`/orders/${id}`)
    }

    // 更新订单状态（写操作 - XIE写）
    static async updateOrderStatus(id: number, status: Order['status']) {
        return apiRequest.patch(`/orders/${id}/status`, { status })
    }

    // 获取订单统计（读操作 - DU读）
    static async getOrderStats() {
        return apiRequest.get('/orders/stats')
    }

    // 获取退款分页数据（读操作 - DU读）
    static async getRefundPage(params: RefundQuery): Promise<RefundPageResponse> {
        console.log('调用GetRefundPage接口，参数:', params)
        const response = await apiRequest.get('/Refund/GetRefundPage', params)
        return response as unknown as RefundPageResponse
    }

    // 获取退款详情（读操作 - DU读）
    static async getRefundDetail(id: number): Promise<RefundDetail> {
        console.log('调用GetRefundDetail接口，参数:', id)
        const response = await apiRequest.get(`/Refund/GetRefundDetail/${id}`)
        return response as unknown as RefundDetail
    }

    // 获取药品订单分页数据（读操作 - DU读）
    static async getDrugOrdersPage(params: DrugOrderQuery): Promise<any> {
        console.log('调用GetDrugOrdersPage接口，参数:', params)
        const response = await apiRequest.get('/DrugOrders/GetDrugOrdersPage', params)
        return response
    }

    // 获取药品订单详情（读操作 - DU读）
    static async getDrugOrderDetail(orderId: number): Promise<any> {
        console.log('调用GetDrugOrderDetail接口，参数:', orderId)
        console.log('完整API路径:', `/DrugOrders/GetDrugOrdersDetail/${orderId}`)
        const response = await apiRequest.get(`/DrugOrders/GetDrugOrdersDetail/${orderId}`)
        return response
    }

    // 处方订单接口类型定义
    static async getPrescriptionOrdersPage(params: any): Promise<any> {
        console.log('调用GetPrescriptionOrderPage接口，参数:', params)
        const response = await apiRequest.get('/PrescriptionOrder/GetPrescriptionOrderPage', params)
        return response
    }

    // 处方订单详情接口
    static async getPrescriptionOrderDetail(orderId: number): Promise<any> {
        console.log('调用GetPrescriptionOrderDetail接口，订单ID:', orderId)
        const response = await apiRequest.get(`/PrescriptionOrder/GetPrescriptionOrderDetail/${orderId}`)
        return response
    }

    // 问诊订单分页接口
    static async getInterrogationOrdersPage(params: any): Promise<any> {
        console.log('调用GetInterrogationOrderPage接口，参数:', params)
        const response = await apiRequest.get('/InterrogationOrder/GetInterrogationOrderPage', params)
        return response
    }

    // 问诊订单详情接口
    static async getInterrogationOrderDetail(orderId: number): Promise<any> {
        console.log('调用GetInterrogationOrderDetail接口，订单ID:', orderId)
        const response = await apiRequest.get(`/InterrogationOrder/GetInterrogationOrderDetail/${orderId}`)
        return response
    }

    // 挂号订单接口
    static async getRegistrationOrdersPage(params: any): Promise<any> {
        console.log('获取挂号订单分页:', params)
        return apiRequest.get('/RegistrationOrder/GetRegistrationOrderPage', params)
    }

    static async getRegistrationOrderDetail(orderId: number): Promise<any> {
        console.log('获取挂号订单详情:', orderId)
        return apiRequest.get(`/RegistrationOrder/GetRegistrationOrderDetail/${orderId}`)
    }
} 